﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccessInterface;
using MyBlog.DataEntity;
using MyBlog.Common;

namespace MyBlog.DataAccess
{
    public class ArticleContext : IDALArticle
    {
        IContext _context;
        public ArticleContext(IContext context)
        {
            _context = context;
        }
        public void Add(Tbl_Article entity)
        {
            _context.Add(entity);
        }

        public void Update(Tbl_Article entity)
        {
            _context.Update(entity);
        }

        public void Delete(params object[] primaryKey)
        {
            _context.Delete<Tbl_Article>(primaryKey);
        }

        public void AddRange(IEnumerable<Tbl_Article> entities)
        {
            _context.AddRange(entities);
        }

        public void DeleteRange(IEnumerable<Tbl_Article> entities)
        {
            _context.DeleteRange(entities);
        }

        public void Delete(System.Linq.Expressions.Expression<Func<Tbl_Article, bool>> keySelector)
        {
            _context.Delete(keySelector);
        }

        public Tbl_Article Get(System.Linq.Expressions.Expression<Func<Tbl_Article, bool>> keySelector)
        {
            return _context.Get(keySelector);
        }

        public Tbl_Article Get(params object[] primaryKey)
        {
            return _context.Get<Tbl_Article>(primaryKey);
        }

        public List<Tbl_Article> GetAll(bool isTracking = true)
        {
            return _context.GetAll<Tbl_Article>(isTracking);
        }

        public List<Tbl_Article> GetAll(System.Linq.Expressions.Expression<Func<Tbl_Article, bool>> keySelector, bool isTracking = true)
        {
            return _context.GetAll<Tbl_Article>(keySelector, isTracking);
        }

        public List<Tbl_Article> GetAll(System.Linq.Expressions.Expression<Func<Tbl_Article, bool>> keySelector, int page, int size, out int total, bool isTracking = true)
        {
            return _context.GetAll<Tbl_Article>(keySelector, page, size, out total, isTracking);
        }

        public List<Tuple<int, string>> GetArticlePrevAndNext(int aid)
        {
            var result = new List<Tuple<int, string>>();
            var curr = _context.Get<Tbl_Article>(aid);
            var datas = _context.GetAll<Tbl_Article>(x => x.AccountId.ToLower() == curr.AccountId.ToLower());
            if (datas.Count > 1)
            {
                var prev = (from p in datas where aid > p.ID && p.ID == datas.Where(x => x.ID < aid).Max(x => x.ID) select p).FirstOrDefault() ?? new Tbl_Article();
                var next = (from p in datas where aid < p.ID && p.ID == datas.Where(x => x.ID > aid).Min(x => x.ID) select p).FirstOrDefault() ?? new Tbl_Article();
                result.Add(new Tuple<int, string>(prev.ID, prev.Title.HtmlDecode()));
                result.Add(new Tuple<int, string>(next.ID, next.Title.HtmlDecode()));
            }
            return result;
        }
    }
}
